package com.yzt.zhuju.utils;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.vectordrawable.graphics.drawable.Animatable2Compat;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;

/**
 * author : bob
 * date   : 2022/12/2
 * desc   :
 */
public class GiftUtils {

    public static void showGiftAnimation(String gifUrl, Activity mActivity, ImageView mImageView, OnGiftStatus mOnGiftStatus) {

        Uri mUri = Uri.parse(gifUrl);
        Glide.with(mActivity).load(mUri)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        ((GifDrawable) resource).setLoopCount(1);//只播放一次
                        // 计算动画时长
                        GifDrawable drawable = (GifDrawable) resource;
                        drawable.registerAnimationCallback(new Animatable2Compat.AnimationCallback() {
                            @Override
                            public void onAnimationStart(Drawable drawable) {
                                super.onAnimationStart(drawable);
                                if (mOnGiftStatus != null) {
                                    mOnGiftStatus.onStart();
                                }
                            }

                            @Override
                            public void onAnimationEnd(Drawable drawable) {
                                super.onAnimationEnd(drawable);
                                if (mOnGiftStatus != null) {
                                    mOnGiftStatus.onEnd();
                                }
                            }
                        });
                        return false;
                    }
                }).into(mImageView);
    }

    public static void showGiftAnimation(int resourceId, Activity mActivity, ImageView mImageView, OnGiftStatus mOnGiftStatus) {

        Glide.with(mActivity).load(resourceId)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        ((GifDrawable) resource).setLoopCount(1);//只播放一次
                        // 计算动画时长
                        GifDrawable drawable = (GifDrawable) resource;
                        drawable.registerAnimationCallback(new Animatable2Compat.AnimationCallback() {
                            @Override
                            public void onAnimationStart(Drawable drawable) {
                                super.onAnimationStart(drawable);
                                if (mOnGiftStatus != null) {
                                    mOnGiftStatus.onStart();
                                }
                            }

                            @Override
                            public void onAnimationEnd(Drawable drawable) {
                                super.onAnimationEnd(drawable);
                                if (mOnGiftStatus != null) {
                                    mOnGiftStatus.onEnd();
                                }
                            }
                        });
                        return false;
                    }
                }).into(mImageView);
    }

    public interface OnGiftStatus {

        void onStart();

        void onEnd();
    }
}
